<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="任务名称" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>

    <el-form-item label="模板名称" prop="tempId">
      <el-select v-model="dataForm.tempId" clearable placeholder="请选择">
        <el-option
          v-for="item in tempList"
          :key="item.tempId"
          :label="item.name"
          :value="item.tempId">
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="填报要求" prop="taskRequire">
      <el-input v-model="dataForm.taskRequire" placeholder="填报要求"></el-input>
    </el-form-item>

    <el-form-item label="上报时间节点" prop="nodeDate">
      <el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="选择日期" v-model="dataForm.nodeDate" style="width: 100%;"></el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        tempList: [],
        dataForm: {
          id: 0,
          name: '',
          tempId: '',
          nodeDate: '',
          sendStatus: '',
          sendTime: '',
          createTime: '',
          taskRequire: '',
        },
        dataRule: {
          name: [
            { required: true, message: '任务名称不能为空', trigger: 'blur' }
          ],
          tempId: [
            { required: true, message: '模板不能为空', trigger: 'blur' }
          ],
          nodeDate: [
            { required: true, message: '上报时间节点不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0

        this.$http({
          url: this.$http.adornUrl('/frt/task/selectTemp'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.tempList = data.list
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/frt/task/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.task.name
                this.dataForm.tempId = data.task.tempId
                this.dataForm.nodeDate = data.task.nodeDate
                this.dataForm.taskRequire = data.task.taskRequire
              }
            })
          }
        })

      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/frt/task/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'tempId': this.dataForm.tempId,
                'nodeDate': this.dataForm.nodeDate,
                'taskRequire': this.dataForm.taskRequire,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
